/*
*  ChgDoc CMS - GNU General Public License, version 3 (GPL-3.0)
*  Copyright (C)  2012. Ian Quigley
*  
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*  
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*/


var editInPlace = new function () {

    var internal = {
        divSelector: ''
    };

    this.forDiv = function (divSelector) {
        internal.divSelector = divSelector;

        $(divSelector + ' *').dblclick(function () { hotEdit(); });

        $(divSelector).mousemove(function (e) {

            switchFocusTo( $(document.elementFromPoint( e.pageX - document.body.scrollLeft, e.pageY - document.body.scrollTop)) );
        });
    };

    hotEdit = function () {

        if (focus_element === undefined)
            return;

        var myDialog = new editHtmlElement({
            element: focus_element.get(0),
			editHook: myEditHook
        });

        switchFocusTo();
        myDialog.show();
    };


    var focus_element;
    var focus_style;
    switchFocusTo = function (ctrl) {

        if (focus_element === ctrl)
            return;

        $('.hightlightElement').removeClass('hightlightElement');
        
        focus_element = ctrl;
        if (focus_element !== undefined) {
            focus_element.addClass('hightlightElement');
        }
    }

    hoverIn = function () {
        switchFocusTo($(this));
    };

    hoverOut = function () {
        switchFocusTo();
    };

    getBackground = function (jqueryElement) {

        if (jqueryElement.is("body") || jqueryElement.is("html"))
            return false;

        var color;
        try {
            color = jqueryElement.css("background-color");
        }
        catch (err) {
            return false;
        }

        if ((color !== 'rgba(0, 0, 0, 0)') && (color !== 'transparent')) {
            return color;
        }

        // call getBackground with parent item
        return getBackground(jqueryElement.parent());

    }
};


	saveArticle = function () {
		
		var artId = $('#article').attr('data-articleid');
		var body = escape($('#article').html());
		
		doAjax('http://localhost/chgdoc/service/Article.asmx/SetBody', '{ articleId : ' + artId + ', body : "' +body + '" }', function (msg) { alert(msg); });
	};

